package com.meusdrinks.view;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;

import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.annotation.ViewScoped;
import br.gov.frameworkdemoiselle.template.AbstractEditPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;

import com.meusdrinks.business.DrinkBC;
import com.meusdrinks.business.IngredienteBC;
import com.meusdrinks.business.UsuarioBC;
import com.meusdrinks.domain.Drink;
import com.meusdrinks.domain.Ingrediente;

@ManagedBean
@ViewScoped
@PreviousView("./drink_list.jsf")
public class DrinkEditMB extends AbstractEditPageBean<Drink, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private DrinkBC drinkBC;
	
	@Inject
	private IngredienteBC ingredienteBC;
	
	@Inject
	private UsuarioBC usuarioBC;
	
	private List<Ingrediente> ingredientesSelecionados;
	
	private List<Ingrediente> ingredientes;
	
	private Integer nota;
	
	public String avaliar(){
		handleLoad();
		getBean().setSomaNotas(getBean().getSomaNotas() + nota);
		drinkBC.update(getBean());
		return "ranking.jsf";
	}
	
	
	@PostConstruct
	private void init(){
		ingredientes = ingredienteBC.findAll();
		ingredientesSelecionados = new ArrayList<Ingrediente>();
		if (!isUpdateMode()){
			getBean().setSomaNotas(0);
			getBean().setQtdAvaliacoes(0);
			getBean().setCriador(usuarioBC.load(1l));
		}
	}
	
	
	@Override
	@Transactional
	public String delete() {
		this.drinkBC.delete(getId());
		return getPreviousView();
	}
	
	@Override
	@Transactional
	public String insert() {
		//getBean().setIngredientes(ingredientesSelecionados);
		this.drinkBC.insert(getBean());
		return getPreviousView();
	}
	
	@Override
	@Transactional
	public String update() {
		//getBean().setIngredientes(ingredientesSelecionados);
		this.drinkBC.update(getBean());
		return getPreviousView();
	}
	
	@Override
	protected void handleLoad() {
		setBean(this.drinkBC.load(getId()));
	}

	public List<Ingrediente> getIngredientes() {
		return ingredientes;
	}


	public void setIngredientes(List<Ingrediente> ingredientes) {
		this.ingredientes = ingredientes;
	}

	public List<Ingrediente> getIngredientesSelecionados() {
		return ingredientesSelecionados;
	}


	public void setIngredientesSelecionados(
			List<Ingrediente> ingredientesSelecionados) {
		this.ingredientesSelecionados = ingredientesSelecionados;
	}


	public Integer getNota() {
		return nota;
	}


	public void setNota(Integer nota) {
		this.nota = nota;
	}

}